package com.zxc.search.service.impl;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import com.zxc.search.pojos.ApUserSearch;
import com.zxc.search.pojos.HistorySearchDto;
import com.zxc.search.service.ApUserSearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ApUserSearchServiceImpl implements ApUserSearchService {


    @Resource
    private MongoTemplate template;

    @Async
    @Override
    public void insert(String keyword, Integer userId) {

        Query query = Query.query(Criteria.where("userId").is(userId).and("keyword").is(keyword));
        ApUserSearch userSearch = template.findOne(query, ApUserSearch.class);
        if (userSearch != null) {
            userSearch.setCreatedTime(new Date());
            template.save(userSearch);
            return;
        }

        Query queryList = Query.query(Criteria.where("userId").is(userId)).with(Sort.by(Sort.Direction.DESC, "createdTime"));
        ApUserSearch apUserSearch = new ApUserSearch();
        apUserSearch.setUserId(userId);
        apUserSearch.setKeyword(keyword);
        apUserSearch.setCreatedTime(new Date());
        List<ApUserSearch> apUserSearches = template.find(queryList, ApUserSearch.class);

        if (apUserSearches.size() < 10) {
            template.save(apUserSearch);
        } else {
            ApUserSearch LastApUserSearch = apUserSearches.get(apUserSearches.size() - 1);
            template.findAndReplace(Query.query(Criteria.where("id").is(LastApUserSearch.getId())), apUserSearch);
        }


    }

    @Override
    public ResponseResult findUserSearch() {

        ApUser apUser = AppThreadLocalUtil.getUser();

        if (apUser == null) return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        List<ApUserSearch> apUserSearches = template.find(Query.query(Criteria.where("userId").is(apUser.getId())).with(Sort.by(Sort.Direction.DESC, "createdTime")), ApUserSearch.class);
        return ResponseResult.okResult(apUserSearches);
    }

    @Override
    public ResponseResult delUserSearch(HistorySearchDto dto) {

        if (dto.getId() == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        ApUser apUser = AppThreadLocalUtil.getUser();

        if (apUser == null) return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);


        template.remove(Query.query(Criteria.where("id").is(dto.getId()).and("userId").is(apUser.getId())), ApUserSearch.class);


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
